package com.education.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.education.common.pojo.ResultVO;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Administrator
 * @create: 2020/5/18 14:47
 **/
@Component
public class HttpUtils {

    @Autowired
    private RestTemplate restTemplate;

    /*
     * 向目的URL发送post请求
     * @param url       目的url
     * @param params    发送的参数
     * @return  ResultVO
     */
    public static ResultVO sendPostRequest(String url, Map<String, String> params, String token) {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<ResultVO> response = client.exchange(url, method, requestEntity, ResultVO.class);
        return response.getBody();
    }

    public ResultVO HttpRestClient(String url, HttpMethod method, JSONObject json) {
        /*SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10*1000);
        requestFactory.setReadTimeout(10*1000);
        RestTemplate client = new RestTemplate(requestFactory);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> requestEntity = new HttpEntity<String>(json.toString(), headers);
        //  执行HTTP请求
        ResponseEntity<ResultVO> response = client.exchange(url, method, requestEntity, ResultVO.class);
        return response.getBody();*/


        /*SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10*1000);
        requestFactory.setReadTimeout(10*1000);
        RestTemplate client = new RestTemplate(requestFactory);*/
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> requestEntity = new HttpEntity<String>(json.toString(), headers);
        //  执行HTTP请求
        ResponseEntity<ResultVO> response = restTemplate.exchange(url, method, requestEntity, ResultVO.class);
        return response.getBody();
    }

    public ResultVO HttpRestClientHeaers(String url, HttpMethod method, JSONObject json, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token);
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> requestEntity = new HttpEntity<String>(json.toString(), headers);
        //执行HTTP请求
        ResponseEntity<ResultVO> response = restTemplate.exchange(url, method, requestEntity, ResultVO.class);
        return response.getBody();
    }

    public static ResultVO HttpRestClientTwo(String url, HttpMethod method, JSONObject json, String token) {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10 * 1000);
        requestFactory.setReadTimeout(10 * 1000);
        RestTemplate client = new RestTemplate(requestFactory);
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token);
        headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8);
        HttpEntity<String> requestEntity = new HttpEntity<String>(json.toString(), headers);
        //  执行HTTP请求
        ResponseEntity<ResultVO> response = client.exchange(url, method, requestEntity, ResultVO.class);
        return response.getBody();
    }

    /**
     * 向指定 URL 发送GET方法的请求
     *
     * @param url   发送请求的 URL
     * @param param 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendTokenGet(String url, String param, String token) {  //httpClient
        HttpClient httpClient = new DefaultHttpClient();
        // get method
        HttpGet httpGet = new HttpGet(url + "?" + param);

        // set header
        String Au = "Bearer " + token;
        httpGet.setHeader("Authorization", Au);

        //response
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
        } catch (Exception e) {
        }

        //get response into String
        String temp = "";
        try {
            org.apache.http.HttpEntity entity = response.getEntity();
            temp = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
        }

        return temp;
    }

    public static void main(String args[]) {
        String ur = "http://115.231.108.246:86/vdtms/api/v1/video/record";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("patientId", "patientId");
        params.put("deviceSerial", "MH-DIP2201M0420180828AACH221051992WCVLU");
        params.put("channelNo", 1);
        params.put("action", 1);
//          HttpUtils.sendPostRequest(ur,params,token);
    }
}